package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    Connection conn=null;
    PreparedStatement prep=null;
    ResultSet res=null;
    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) throws Exception{
        try {
            conn=DBUtil.getConnection();
            String sql=" select * from food where businessId=? order by foodId ";
            prep=conn.prepareStatement(sql);

            prep.setInt(1,businessId);
            res=prep.executeQuery();
            List<Food>list=new ArrayList<>();

            while (res.next()){
                Food food=new Food();
                food.setBusinessId(res.getInt("businessId"));
                food.setRemarks(res.getString("remarks"));
                food.setFoodExplain(res.getString("foodExplain"));
                food.setFoodId(res.getInt("foodId"));
                food.setFoodImg(res.getString("foodImg"));
                food.setFoodName(res.getString("foodName"));
                food.setFoodPrice(res.getDouble("foodPrice"));
                list.add(food);


            }
           return list;

        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(res,prep);
        }

    }
}
